// pages/home/gameDetail/index.js
import {
  getActivityDetailApi,
  postActivityApplyApi,
  postActivityCancelApi,
  getActivityApplyUserApi
} from '../../../https/activity'
import {
  BASE_URL
} from '../../../constant/index'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    BASE_URL,
    detail: {},
    userList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const docid = options.docid
    this.getActivityDetai(docid)
  },
  async getUserList(id) {
    const getRes = await getActivityApplyUserApi({
      id
    })
    console.log(getRes)
    if (getRes.code == 200) {
      this.setData({
        userList: getRes.data
      })
    }
  },
  async getActivityDetai(id) {
    console.log(id)

    const getRes = await getActivityDetailApi({
      id
    })
    console.log(getRes)
    if (getRes.code == 200) {
      this.setData({
        detail: getRes.data
      })
      this.getUserList(id)
    } else {
      wx.showToast({
        title: '活动不存在',
        icon: 'error'
      })
    }
  },
  // 报名
  async bindApply() {
    const id = this.data.detail.id
    const {
      confirm
    } = await wx.showModal({
      content: '您确认报名？',
    })
    if (!confirm) return
    const postRes = await postActivityApplyApi({
      id
    })
    console.log(postRes)
    if (postRes.code == 200) {
      wx.showModal({
        title: '温馨提示',
        content: '报名成功！',
        showCancel: false,
        complete: (res) => {
          this.getActivityDetai(id)
        }
      })
    } else {
      wx.showToast({
        title: postRes.message,
        icon: 'none'
      })
    }
  },
  // 取消报名
  async bindCancel() {
    const id = this.data.detail.id
    const {
      confirm
    } = await wx.showModal({
      content: '确认取消该活动',
    })
    if (!confirm) return
    const postRes = await postActivityCancelApi({
      id
    })
    console.log(postRes)
    if (postRes.code == 200) {
      wx.showToast({
        title: '已取消',
        icon: 'success'
      })
    } else {
      wx.showToast({
        title: '已取消',
        icon: 'none'
      })
    }
    this.getActivityDetai(id)
  }
})